/**
 * 用户登录
 * @module 用户登录
 * @author 徐灿
 * @date 2019-9-9
  */
'use strict';

const signIn = require('../actions/user/signIn');
const moment = require('moment');

module.exports = async function (ctx) {
    let params = {
        last_login_ip: ctx.ip.slice(7) || 'localhost',
        last_sign_date: moment().format('YYYY-MM-DD HH:mm:ss'),
        login_name: ctx.request.fields.login_name,
        password: ctx.request.fields.password
    };

    let user = await signIn(params);
    if (user) {
        ctx.session.user = user;
        ctx.cookies.set('cookieGroupId', user.group_id, {httpOnly: false});
        ctx.redirect('/');
    } else {
        /*  ctx.body = {success: false, message: '用户名密码错误'};、*/
        ctx.redirect('/?isLogin=0');
    }
};